﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Speech.Synthesis;

namespace DeviceUnion.Voice
{
    public class VoiceHelper
    {
        private SpeechSynthesizer Synth { get; set; }
        public List<InstalledVoice> InstalledVoices { get; set; }

        public int Rate { get; set; }
        public int Volume { get; set; }
        public List<string> Voices { get; set; }

        public VoiceHelper()
        {
            Rate = 0;
            Volume = 80;
        }

        public List<string> GetInstalledVoices()
        {
            if (InstalledVoices != null)
            {
                var lis = new List<string>();
                InstalledVoices.ForEach(v => lis.Add(v.VoiceInfo.Name));
                return lis;
            }
            return null;
        }

        public void RefredVoices()
        {
            if (Synth != null)
            {
                try
                {
                    var lis = Synth.GetInstalledVoices();
                    InstalledVoices = new List<InstalledVoice>();
                    lis.ToList().ForEach(v => InstalledVoices.Add(v));
                    Voices = new List<string>();
                    InstalledVoices.ForEach(v => Voices.Add(v.VoiceInfo.Name));
                    Rate = Synth.Rate;
                    //使用 synth 设置朗读音量 [范围 0 ~ 100]
                    Volume = Synth.Volume;
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.Message);
                }
            }
        }

        public void Close()
        {
            if (Synth != null)
                Synth.Dispose();
        }

        public void InitVoice()
        {
            //获取系统已安装的语音库列表
            try
            {
                Synth = new SpeechSynthesizer();
                Rate = Synth.Rate;
                //使用 synth 设置朗读音量 [范围 0 ~ 100]
                Volume = Synth.Volume;
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                Synth = null;
            }
        }

        public void ApplyVoice(InstalledVoice voice)
        {
            if (voice.Enabled)
            {
                Synth.SelectVoice(voice.VoiceInfo.Name);
            }
            else
            {
                throw new Exception("声音不可用：" + voice.VoiceInfo.Name);
            }
        }

        public void ApplyVoice(string voice)
        {
            if (voice != null)
            {
                Synth.SelectVoice(voice);
            }
            else
            {
                throw new Exception("声音不可用：" + voice);
            }
        }

        public void SpeakText(string text)
        {
            try
            {
                //使用 synth 设置朗读频率 [范围 -10 ~ 10]
                Synth.Rate = Rate;
                //使用 synth 设置朗读音量 [范围 0 ~ 100]
                Synth.Volume = Volume;
                //如果未启用，启用
                if (Synth.State != SynthesizerState.Ready)
                {
                    Synth.Resume();
                }
                //将文本合成语音
                Synth.Speak(text);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }
        }
    }
}